Code for Facebook
Write object-oriented code to implement the design of the Facebook problem.
We'll cover the following
We’ve reviewed different aspects of Facebook and observed the attributes attached to the problem using various UML diagrams. Let’s now explore the more practical side of things, where we will work on implementing the Facebook network using multiple languages. This is usually the last step in an object-oriented design interview process.
We have chosen the following languages to write the skeleton code of the different classes present in Facebook:
Java
C#
Python
C++
JavaScript
Facebook classes#
In this section, we will provide the skeleton code of the classes designed in the class diagram lesson.
Note: For simplicity, we are not defining getter and setter functions. The reader can assume that all class attributes are private and accessed through their respective public getter methods and modified only through their public method functions.
Constants#
The following code provides the definition of the various enums and custom data types being used in the Facebook design:
Note: JavaScript does not support enumerations so we will be using the
Object.freeze()method as an alternative that freezes an object and prevents further modifications.
Interfaces implemented by user#
Facebook will have several interfaces that will be implemented by users and are described below:
PageFunctionsByUser: This will define the functions that a user will perform while interacting with pages.GroupFunctionsByUser: This will define the functions that a user will perform while interacting with groups.PostFunctionsByUser: This will define the functions that a user will perform while interacting with posts.CommentFunctionsByUser: This will define the functions that a user will perform while interacting with comments.
The definition of all these interfaces is given below:
Account#
The Account class refers to an account of a user on Facebook and is responsible for containing their personal details, such as username, password, etc. It also allows users to reset their existing passwords. The definition of this class is given below:
Person, user, and admin#
The Person class will be an abstract class that represents a normal Facebook user. A User can also be an Admin. The definition of these two classes is provided below:
Note: The
Userclass will implement the interfaces mentioned above. A few of these have been mentioned in the code below.
Profile, education, places, and work#
The Work, Education, and Places classes will provide a user's personal information and will make up the Profile class. The definition of these classes is given below:
Page, post, and comment#
Facebook users can create and like pages, posts, and comments. The definition of these classes is given below:
Profile privacy#
Each profile will have its own privacy settings where users can change the visibility settings of their friends' list, and lock their profile and profile picture.
Group and the group functions interface#
Facebook users can also create and join groups as well as add new and delete new users. In addition, users that initially join groups will be notified of all new activities in the groups. The definition of these classes is given below:
Message and friend request#
Each Facebook user can send messages and friend requests to other users. The definition of both of these classes is given below:
Notification#
The Notification class is responsible for sending notifications to users about any new messages, comments, posts, or friend requests via the built-in notification option. Its definition is given below:
Search catalog and interface#
The SearchCatalog class contains information on members, groups, pages, and various posts. It also implements the Search interface class to enable the search functionality based on the given criteria (member, group, page names, and post keywords). The definition of these two classes is given below:
Wrapping up#
We've explored the complete design of Facebook in this chapter. We've looked at how Facebook can be visualized using various UML diagrams and designed using object-oriented principles and design patterns.
Activity Diagram for Facebook
Getting Ready: An Online Stock Brokerage System